package TUILayer;

import ControlLayer.DvdCtr;
import ModelLayer.*;

import java.util.Scanner;
import java.util.ArrayList;

/**
 * @author (Gruppe C) 
 * @version (2012.11.09)
 */

public class DvdUI
{
    // Fields
    private DvdCtr dvdCtr; //DVD UI must know the DVD controller 
    
    // Ctor
    public DvdUI()
    {
        start(DvdCtr.getInstance());
    }
    
    public void start(DvdCtr ctr)
    {
        dvdCtr = ctr;
        dvdMenu();
    }
    
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise =   writeDvdMenu();
            if (choise == 1) // Show DVD info
            {
                findDvd();
            }
            else if(choise == 2) // Create DVD description
            {
                createDvd();
            }
            else if(choise == 3)  // Create DVD specimen
            {
                createCopy();
            }
            else if(choise == 4) // Delete DVD
            {
                deleteDvd();
            }
            else if(choise == 5) // Show current DVD list
            {
                listAllDvds();
            }
            else
            {
                exit = true;
            }
        }//end while
    }
    private int writeDvdMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** DVD Menu ***");
        System.out.println(" (1) Show DVD information");
        System.out.println(" (2) Create DVD description");
        System.out.println(" (3) Create DVD specimen");
        System.out.println(" (4) Delete DVD");
        System.out.println(" (5) Show current DVD list");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choise: ");
        
        int choise = keyboard.nextInt();
        return choise;
    }
    
    /*
     * Search for a dvd.
     */
    private void findDvd()
    {
        // Get dvd number from user input.
        int no = MainMenuUI.getIntInput("Type DVD id: ");
        // Find the dvd by number.
        Dvd dvd = dvdCtr.findDvd(no);
        
        if (dvd != null)
        {
            dvd.print();
        }
        else
        {
            System.out.println("DVD not found!");
        }
        
        MainMenuUI.waitForInput();
    }
    
    /*
     * Create a dvd.
     */
    private void createDvd()
    {
        String title = MainMenuUI.getStringInput("Type DVD title: ");
        String artist = MainMenuUI.getStringInput("Type DVD artist: ");
        String releaseDate = MainMenuUI.getStringInput("Type DVD release date: ");
        
        dvdCtr.createDvd(title, artist, releaseDate);
    }
    
    /*
     * Create a specimen of a dvd (copy).
     */
    private void createCopy()
    {
        int id = MainMenuUI.getIntInput("Type DVD id: ");
        
        Dvd dvd = dvdCtr.findDvd(id);
        if (dvd != null)
        {
            String serialNr = MainMenuUI.getStringInput("Type DVD Specimen serial nr: ");
            String availableDate = MainMenuUI.getStringInput("Type DVD available date: ");
            String availablePrice = MainMenuUI.getStringInput("Type DVD available price: ");
            
            dvdCtr.createCopy(id, serialNr, availableDate, availablePrice);
        }
        else
        {
            System.out.println("DVD not found!");
        }
        
        MainMenuUI.waitForInput();
    }
    
    /*
     * Delete dvd.
     */
    private void deleteDvd()
    {
        int no = MainMenuUI.getIntInput("Type DVD number: ");
        
        if (!dvdCtr.deleteDvd(no))
        {
            System.out.println("DVD not found!");
            MainMenuUI.waitForInput();
        }
    }
    
    /*
     * Show all dvd's and their copies.
     */
    private void listAllDvds()
    {
        ArrayList<Dvd> dvds = dvdCtr.listAllDvds();
        
        if (!dvds.isEmpty())
        {
            // Go through all dvds.
            for (Dvd dvd : DvdContainer.getInstance().getAllDvds())
            {
                // Print dvd info.
                dvd.print();
                
                System.out.println("Copies:");
                
                // Go through all copies for the current dvd and print info.
                for (Specimen specimen : dvd.getAllSpecimens())
                {
                    specimen.print();
                }
            }
        }
        else
        {
            System.out.println("DVD list is empty!");
        }
        
        MainMenuUI.waitForInput();
    }
}